package com.java.test;

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TempConverter {
	private JTextField field = new JTextField();
	private JButton toCelsius = new JButton(" To Celsius  ");
	private JButton toFahrenheit = new JButton("To Fahrenheit");

	private class TextPanel extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 6600955275561994396L;

		public TextPanel() {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			add(Box.createRigidArea(new Dimension(5, 0)));
			add(field);
			add(Box.createRigidArea(new Dimension(5, 0)));
		}
	}

	private class ButtonPanel extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = -2583610512456711288L;

		public ButtonPanel() {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			add(Box.createRigidArea(new Dimension(5, 0)));
			add(toCelsius);
			add(Box.createRigidArea(new Dimension(5, 0)));
			add(toFahrenheit);
			add(Box.createRigidArea(new Dimension(5, 0)));
		}
	}

	private class TempConverterPanel extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = -8950375754855610531L;

		public TempConverterPanel() {
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			TextPanel txtPanel = new TextPanel();
			ButtonPanel buttonPanel = new ButtonPanel();
			toCListener clistener = new toCListener();
			toFListener flistener = new toFListener();
			toCelsius.addItemListener(clistener);
			toFahrenheit.addItemListener(flistener);
			add(Box.createRigidArea(new Dimension(0, 5)));
			add(txtPanel);
			add(Box.createRigidArea(new Dimension(0, 5)));
			add(buttonPanel);
			add(Box.createRigidArea(new Dimension(0, 5)));
		}
	}

	private class toCListener implements ItemListener {
		public void itemStateChanged(ItemEvent event) {
			String ans = "";
			String text = field.getText();
			double f = Double.parseDouble(text);
			ans = fINTOc(f);
			field.setText(ans);
		}
	}

	private class toFListener implements ItemListener {
		public void itemStateChanged(ItemEvent event) {
			String ans = "";
			String text = field.getText();
			double c = Double.parseDouble(text);
			ans = cINTOf(c);
			field.setText(ans);
		}
	}

	public String cINTOf(double c) {
		double f = ((1.8 * c) + 32);
		String ans = ("" + f);
		return ans;
	}

	public String fINTOc(double f) {
		double c = ((f / 1.8) - 32);
		String ans = ("" + c);
		return ans;
	}

	@SuppressWarnings("deprecation")
	public void run() {
		JFrame frame = new JFrame("Temp Converter");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		TempConverterPanel panel = new TempConverterPanel();
		/*BufferedImage image = null;
		try {
			System.out.println("path: " + frame.getClass().getResource("/temp.gif").getPath());
			File file = new File("C:\temp.gif");
			image = ImageIO.read(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		frame.setIconImage(image);*/
		frame.add(panel);
		frame.pack();
		frame.setResizable(false);
		frame.show();
	}

	public static void main(String[] args) {
		TempConverter converter = new TempConverter();
		converter.run();
	}
}